﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class ePioneer_CourseInfo
    {
        readonly LYM.CommonWeb.DataServer.DataBase data = new LYM.CommonWeb.DataServer.DataBase(LYM.CommonWeb.ConfigHelper.GetConfigString("EMPC"), false);
        //DataBase data = new DataBase();
        public ePioneer_CourseInfo()
        { }
        #region  Method

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.ePioneer_CourseInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ePioneer_CourseInfo set ");
            strSql.Append("Title=@Title,");
            strSql.Append("CourseType=@CourseType,");
            strSql.Append("SolutionType=@SolutionType,");
            strSql.Append("Lecturer=@Lecturer,");
            strSql.Append("MainContent=@MainContent,");
            strSql.Append("BriefInfo=@BriefInfo,");
            strSql.Append("IReservation=@IReservation,");
            strSql.Append("Attachment=@Attachment,");
            strSql.Append("AttachmentName=@AttachmentName,");
            strSql.Append("CourseStartDate=@CourseStartDate,");
            strSql.Append("StartTime=@StartTime,");
            strSql.Append("EndTime=@EndTime,");
            strSql.Append("CreateDate=@CreateDate,");
            strSql.Append("IsCheck=@IsCheck,");
            strSql.Append("ICount=@ICount,");
            strSql.Append("IsLine=@IsLine,");
            strSql.Append("Url=@Url");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.NVarChar,50),
					new SqlParameter("@CourseType", SqlDbType.NVarChar,50),
					new SqlParameter("@SolutionType", SqlDbType.NVarChar,50),
					new SqlParameter("@Lecturer", SqlDbType.Int,4),
					new SqlParameter("@MainContent", SqlDbType.NVarChar),
					new SqlParameter("@BriefInfo", SqlDbType.NVarChar),
					new SqlParameter("@IReservation", SqlDbType.Int,4),
					new SqlParameter("@Attachment", SqlDbType.NVarChar,1000),
					new SqlParameter("@AttachmentName", SqlDbType.NVarChar,50),
					new SqlParameter("@CourseStartDate", SqlDbType.DateTime),
					new SqlParameter("@StartTime", SqlDbType.NVarChar,50),
					new SqlParameter("@EndTime", SqlDbType.NVarChar,50),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@IsCheck", SqlDbType.Int,4),
					new SqlParameter("@ICount", SqlDbType.Int,4),
                    new SqlParameter("@IsLine", SqlDbType.Int,4),
                    new SqlParameter("@Url", SqlDbType.NVarChar,1000),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.Title == null ? "" : model.Title;
            parameters[1].Value = model.CourseType == null ? "" : model.CourseType;
            parameters[2].Value = model.SolutionType == null ? "" : model.SolutionType;
            parameters[3].Value = model.Lecturer == null ? 0 : model.Lecturer;
            parameters[4].Value = model.MainContent == null ? "" : model.MainContent;
            parameters[5].Value = model.BriefInfo == null ? "" : model.BriefInfo;
            parameters[6].Value = model.IReservation == null ? 0 : model.IReservation;
            parameters[7].Value = model.Attachment == null ? "" : model.Attachment;
            parameters[8].Value = model.AttachmentName == null ? "" : model.AttachmentName;
            parameters[9].Value = model.CourseStartDate == null ? DateTime.Now : model.CourseStartDate;
            parameters[10].Value = model.StartTime == null ? "" : model.StartTime;
            parameters[11].Value = model.EndTime == null ? "" : model.EndTime;
            parameters[12].Value = model.CreateDate == null ? DateTime.Now : model.CreateDate;
            parameters[13].Value = model.IsCheck == null ? 0 : model.IsCheck;
            parameters[14].Value = model.ICount == null ? 0 : model.ICount;
            parameters[15].Value = model.IsLine == null ? 0 : model.IsLine;
            parameters[16].Value = model.Url == null ? "" : model.Url;
            parameters[17].Value = model.Id;

            int rows = 0;
            data.ProExecuteCommand(strSql.ToString(), false, ref rows, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.ePioneer_CourseInfo GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from ePioneer_CourseInfo ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = Id;

            Model.ePioneer_CourseInfo model = new Model.ePioneer_CourseInfo();
            DataTable dt = new DataTable();
            data.GetDataTable(strSql.ToString(), false,ref dt, parameters);
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["Id"] != null && dt.Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(dt.Rows[0]["Id"].ToString());
                }
                if (dt.Rows[0]["Title"] != null && dt.Rows[0]["Title"].ToString() != "")
                {
                    model.Title = dt.Rows[0]["Title"].ToString();
                }
                if (dt.Rows[0]["CourseType"] != null && dt.Rows[0]["CourseType"].ToString() != "")
                {
                    model.CourseType = dt.Rows[0]["CourseType"].ToString();
                }
                if (dt.Rows[0]["SolutionType"] != null && dt.Rows[0]["SolutionType"].ToString() != "")
                {
                    model.SolutionType = dt.Rows[0]["SolutionType"].ToString();
                }
                if (dt.Rows[0]["Lecturer"] != null && dt.Rows[0]["Lecturer"].ToString() != "")
                {
                    model.Lecturer = int.Parse(dt.Rows[0]["Lecturer"].ToString());
                }
                if (dt.Rows[0]["MainContent"] != null && dt.Rows[0]["MainContent"].ToString() != "")
                {
                    model.MainContent = dt.Rows[0]["MainContent"].ToString();
                }
                if (dt.Rows[0]["BriefInfo"] != null && dt.Rows[0]["BriefInfo"].ToString() != "")
                {
                    model.BriefInfo = dt.Rows[0]["BriefInfo"].ToString();
                }
                if (dt.Rows[0]["IReservation"] != null && dt.Rows[0]["IReservation"].ToString() != "")
                {
                    model.IReservation = int.Parse(dt.Rows[0]["IReservation"].ToString());
                }
                if (dt.Rows[0]["Attachment"] != null && dt.Rows[0]["Attachment"].ToString() != "")
                {
                    model.Attachment = dt.Rows[0]["Attachment"].ToString();
                }
                if (dt.Rows[0]["AttachmentName"] != null && dt.Rows[0]["AttachmentName"].ToString() != "")
                {
                    model.AttachmentName = dt.Rows[0]["AttachmentName"].ToString();
                }
                if (dt.Rows[0]["CourseStartDate"] != null && dt.Rows[0]["CourseStartDate"].ToString() != "")
                {
                    model.CourseStartDate = DateTime.Parse(dt.Rows[0]["CourseStartDate"].ToString());
                }
                if (dt.Rows[0]["StartTime"] != null && dt.Rows[0]["StartTime"].ToString() != "")
                {
                    model.StartTime = dt.Rows[0]["StartTime"].ToString();
                }
                if (dt.Rows[0]["EndTime"] != null && dt.Rows[0]["EndTime"].ToString() != "")
                {
                    model.EndTime = dt.Rows[0]["EndTime"].ToString();
                }
                if (dt.Rows[0]["CreateDate"] != null && dt.Rows[0]["CreateDate"].ToString() != "")
                {
                    model.CreateDate = DateTime.Parse(dt.Rows[0]["CreateDate"].ToString());
                }
                if (dt.Rows[0]["IsCheck"] != null && dt.Rows[0]["IsCheck"].ToString() != "")
                {
                    model.IsCheck = int.Parse(dt.Rows[0]["IsCheck"].ToString());
                }
                if (dt.Rows[0]["ICount"] != null && dt.Rows[0]["ICount"].ToString() != "")
                {
                    model.ICount = int.Parse(dt.Rows[0]["ICount"].ToString());
                }
                if (dt.Rows[0]["IsLine"] != null && dt.Rows[0]["IsLine"].ToString() != "")
                {
                    model.IsLine = int.Parse(dt.Rows[0]["IsLine"].ToString());
                }
                if (dt.Rows[0]["Url"] != null && dt.Rows[0]["Url"].ToString() != "")
                {
                    model.Url = dt.Rows[0]["Url"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM ePioneer_CourseInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DataSet ds = new DataSet();
            data.GetDataSet(strSql.ToString(), false,ref ds, null);
            return ds;
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM ePioneer_CourseInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            DataSet ds = new DataSet();
            data.GetDataSet(strSql.ToString(), false, ref ds, null);
            return ds;
        }

        #endregion  Method

        #region 自定义方法
        /// <summary>
        /// 获取课程信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetCourseInfo(string keyWord, string courseType, string lecturer, DateTime startDate, DateTime endDate)
        {
            SqlParameter[] values = new SqlParameter[5]{
                new SqlParameter("@Title", keyWord),
                new SqlParameter("@CourseType",courseType),
                new SqlParameter("@Lecturer",lecturer),
                new SqlParameter("@StartDate",startDate),
                new SqlParameter("@EndDate",endDate)};
            DataTable dt = new DataTable();
            data.GetDataTable("usp_GetCourseInfo", true,ref dt, values);
            return dt;
        }
        #endregion
    }
}
